import bpy

data_dictionary = '/mnt/storage/Projects/3dconomy/data/'
datafile = 'us_data.txt'
# datafile = 'cdn_data.txt'
# datafile = 'eu_data.txt'

def InitFunction():

	line_data_list = []
	dataset = open(data_dictionary + datafile)

	for line in dataset:
		items = line.split(",")
		line_data_list.append(items)
	dataset.close()

	line_data_list.sort(compare)
	
	InflateObjects(line_data_list)
	

def compare(a, b):
	return cmp(float(b[1]), float(a[1]))

def InflateObjects (data_set):
	#set red for top 10%, green for end 10%, others yellow
	n=len(data_set)
	j = n/10
	m = n - j
	u=0

	
	for data_segment in data_set:
		r=1.0
		g=1.0
		b=0.0
	
		if u < j:
			r=1.0
			g=0.0
		elif u > m:
			r=0.0
			g=1.0
		print(data_segment[0])
		print(data_segment[1])
		
		#1. rename materials to state abbrev
		#2. mat = bpy.data.materials[data_segment[0]]
		#2.5 calculate the correct color for the object.
		#3. mat.properties['YafRay']['color'][0] = 0.3
		#   mat.properties['YafRay']['color'][1] = 0.3
		#   mat.properties['YafRay']['color'][2] = 0.3

# scale the state/prov				
		target_object = bpy.data.objects[data_segment[0]]		
		target_object.SizeZ = abs(float(data_segment[1]))

# ok scaling is done

# Get the material for the state/prov		
		mat = bpy.data.materials[data_segment[0]]
		mat.properties['YafRay']['color'][0] = r
		mat.properties['YafRay']['color'][1] = g
		mat.properties['YafRay']['color'][2] = b

		u=u+1
#	for mat in  bpy.data.materials:
#		print (mat.name)
#		if mat.properties.has_key("YafRay"):
#			print mat.properties['YafRay']['color'][0]
#			print mat.properties['YafRay']['color'][1]
#			print mat.properties['YafRay']['color'][2]

			#for prop in mat.properties['YafRay']:
				#print prop
				#print prop.value
		
InitFunction()